package com.shijun.tankwar;

import android.content.Context;
import android.util.AttributeSet;

/**
 * Created by zhushijun on 2018/1/12 0012.
 */

public class ControlOrientationView extends ControlMoveView {

    public ControlOrientationView(Context context, int x, int y, int width, int height) {
        super(context, x, y, width, height);
    }

    public ControlOrientationView(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

    public ControlOrientationView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected double getOrientationOnMaxOrientationLimit(double oldOrientation, double newOrientation, double maxMoveOrientation) {
        //如果分别在一四区间，需要把一区间的加上一个周期
        if (oldOrientation <= π * 0.5d && newOrientation >= π * 1.5d) {
            oldOrientation = oldOrientation + π * 2d;
        } else if (oldOrientation >= π * 1.5d && newOrientation <= π * 0.5d) {
            newOrientation = newOrientation + π * 2d;
        }

        double distanceOrientation = oldOrientation - newOrientation;
        boolean isTurnOrientation = false;
//        if (Math.abs(distanceOrientation) >= π * 0.5d) {
//            isTurnOrientation = true;
//            if (newOrientation - π >= 0) {
//                newOrientation = newOrientation - π;
//            } else {
//                newOrientation = newOrientation + π;
//            }
//
//            //位置变化，重新来一遍
//            if (oldOrientation <= π * 0.5d && newOrientation >= π * 1.5d) {
//                oldOrientation = oldOrientation + π * 2d;
//            } else if (oldOrientation >= π * 1.5d && newOrientation <= π * 0.5d) {
//                newOrientation = newOrientation + π * 2d;
//            }
//        }

        if (oldOrientation - newOrientation > maxMoveOrientation) {
            if (oldOrientation - maxMoveOrientation < 0) {
                newOrientation = (float) (π * 2d + (oldOrientation - maxMoveOrientation));
            } else if (oldOrientation - maxMoveOrientation > π * 2d) {
                newOrientation = (float) ((oldOrientation - maxMoveOrientation) - π * 2d);
            } else {
                newOrientation = (float) (oldOrientation - maxMoveOrientation);
            }
        } else if (oldOrientation - newOrientation < -maxMoveOrientation) {
            if (oldOrientation + maxMoveOrientation < 0) {
                newOrientation = (float) (π * 2d + (oldOrientation + maxMoveOrientation));
            } else if (oldOrientation + maxMoveOrientation > π * 2d) {
                newOrientation = (float) ((oldOrientation + maxMoveOrientation) - π * 2d);
            } else {
                newOrientation = (float) (oldOrientation + maxMoveOrientation);
            }
        }

//        if (isTurnOrientation) {
//            if (newOrientation - π >= 0) {
//                tankMoveOrientation = newOrientation - π;
//            } else {
//                tankMoveOrientation = newOrientation + π;
//            }
//        } else {
        tankMoveOrientation = newOrientation;
//        }
        tankOrientation = (float) ((newOrientation / π) * 180);
        return tankOrientation;
    }
}
